import json

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad


def get_key():
    a = 5 * 8
    b = 6 * 706
    d = 517 * 560
    f = 485 * 713
    x = 'a'
    y = 'json.dumps(da'
    return (''.join([str(i) for i in [a, b, d, f, x, y]]) * 1).encode('utf-8')


def get_iv():
    x = 'a'
    y = 'json.dumps(data'
    return (''.join([str(i) for i in [x, y]]) * 1).encode('utf-8')


def enc_data(data: dict):
    data = json.dumps(data, ensure_ascii=True)
    data = data.encode('utf-8')
    password = get_key()
    aes = AES.new(password, AES.MODE_CBC, iv=get_iv())  # 创建一个aes对象
    data = pad(data, 16)
    enc_text = aes.encrypt(data)  # 加密明文
    return enc_text


def dec_data(enc_data: bytes):
    password = get_key()
    aes = AES.new(password, AES.MODE_CBC, iv=get_iv())  # 创建一个aes对象
    data = aes.decrypt(enc_data)
    data = unpad(data, 16)
    return json.loads(data.decode('utf-8'))
